package com.troyhigh.njrotc.admintrackerold;

import java.util.Scanner;
import java.util.ArrayList;
import java.sql.Date;
import java.util.StringTokenizer;
import java.io.*;

public class ATMenuLine
{   
    private static AdminTracker admintracker;
    private static Scanner scanner;
    private static double version = 1.6;
    private static String directory = "/Main";
    
    public static void main(String[] args)
    {
        System.out.println("Welcome to AdminTracker, Command Line (With Menus) Edition!");
        System.out.println("Programmed by BRO Quan Nguyen, c/o 2010!");
        admintracker = new AdminTracker();
        scanner = new Scanner(System.in);
        do { System.out.print("\nAdminTracker> "); } while(getInstruction());
        admintracker.close();
        scanner.close();
        System.out.println("Goodbye!");     
    }
    
    private static boolean getInstruction()
    {
        String command = getCommand();
        if(command.equals("quit") || command.equals("exit") ||
           command.equals("halt") || command.equals("stop") ||
           command.equals("asdf") || command.equals("tran") )
            return false;
        return interpret(command);
    }
    
    private static boolean interpret(String command)
    {
        if(command.equals("help"))
        {
            return help();
        }
        else if(command.equals("student") || command.equals("students"))
        {
            return students();
        }
        else if(command.equals("event") || command.equals("events"))
        {
            return events();
        }
        else if(command.equals("award") || command.equals("awards"))
        {
            return awards();
        }
        else if(command.equals("backup"))
        {
            ATXLibraryDriver.init();
            for(Student s : admintracker.getStudents())
            ATXLibraryDriver.writeLibrary(s, new File("students"+s.getID()+".atx"));
            System.out.println("Backed up.");
        }
        else
        {
            System.out.println("Command not found.");
        }
        return true;
    }
    
    
    private static boolean students()
    {
        do
        {
            System.out.println("Students Menu");
            System.out.println("Verb?");
            System.out.println("1. Get All");
            System.out.println("2. Create");
            System.out.println("3. Edit");
            System.out.println("4. Cancel");
            System.out.println("Verb> ");
        } while (studentLoop());
        return true;
    }
    
    private static boolean studentLoop()
    {
        String command = getCommand();
        if(command.equals("4") || command.equals("cancel") || command.equals("\\c"))
        {
            return false;
        }
        else if(command.equals("1") || command.equals("get all"))
        {
            System.out.println("Printing all students' names and IDs:");
            for(Student s : admintracker.getStudents())
            {
                System.out.println(s.getName() + ", " + s.getID());
            }
            System.out.println("End Student List");
        }
        else if(command.equals("2") || command.equals("create"))
        {
            System.out.print("(1/7) Platoon? ");
            String p = scanner.nextLine();
            if(p.equals("\\c")) return true;
            System.out.print("(2/7) Last Name? ");
            String l = scanner.nextLine();
            if(l.equals("\\c")) return true;
            System.out.print("(3/7) First Name? ");
            String f = scanner.nextLine();
            if(f.equals("\\c")) return true;
            System.out.print("(4/7) ID? ");
            String i = scanner.nextLine();
            if(i.equals("\\c")) return true;
            System.out.print("(5/7) Gender (M/F)? ");
            String m = scanner.nextLine();
            if(m.equals("\\c")) return true;
            System.out.print("(6/7) Grade? ");
            String g = scanner.nextLine();
            if(g.equals("\\c")) return true;
            System.out.print("(7/7) NS Year? ");
            String y = scanner.nextLine();
            if(y.equals("\\c")) return true;
        
            int p2 = Integer.parseInt(p);
            int i2 = Integer.parseInt(i);
            int g2 = Integer.parseInt(g);
            int y2 = Integer.parseInt(y);

            admintracker.createStudent(p2, l, f, i2, m, g2, y2);
            System.out.println("Student successfully created.");
            return true;
        }          
        else if(command.equals("3") || command.equals("edit"))
        {
            System.out.print("(1/1) Student (ID/Name)? ");
            String id = scanner.nextLine();
            if(id.toUpperCase().equals("\\c")) return true;
            id = id.toUpperCase();

            Student s = null;
            if(id.matches("\\d{4,}"))
            {
                s = admintracker.getStudent("", "", Integer.parseInt(id));
            }
            else if(id.matches("(\\w|\\s)++"))
            {
                if(admintracker.getStudents(id, "", -1).size() +
                   admintracker.getStudents("", id, -1).size() == 1)
                {
                    Student sa = admintracker.getStudent(id, "", -1);
                    Student sb = admintracker.getStudent("", id, -1);
                    s = sa;
                    if(s == null) s = sb;
                }
                else
                {
                    System.out.println("Ambiguous name, possible entries below:");
                    ArrayList<Student> a = admintracker.getStudents(id, "", -1);
                    ArrayList<Student> b = admintracker.getStudents("", id, -1);
                    for(Student sp : a)
                        System.out.println(sp);
                    for(Student sq : b)
                        System.out.println(sq);
                    System.out.println("End Student List");
                }
            }
            if(s == null)
            {
                System.out.println("Student doesn't exist, or there are multiple entries!");
                return true;
            }
            do
            {
                System.out.println(s.getName()+", " + s.getID() + ":");
                System.out.println("Student Edit Menu");
                System.out.println("1. Get Info");
                System.out.println("2. Edit Info");
                System.out.println("3. Remove");
                System.out.println("4. Cancel");
                System.out.println("Edit> ");
            } while (studentEdit(s));
        }
        return true;
    }
    
    private static boolean studentEdit(Student s)
    {
        String command = getCommand();
        if(command.equals("4") || command.equals("cancel") || command.equals("\\c"))
        {
            return false;
        }
        else if(command.equals("1") || command.equals("get info"))
        {
            System.out.println("Name: " + s.getName() + " | ID: " + s.getID());
            System.out.println("Platoon: " + s.getGroup() + 
                    " | Grade: " + s.getProperties().getProperty("grade"));
            System.out.println("Gender: " + s.getProperties().getProperty("gender") + 
                    " | NS Year: " + s.getProperties().getProperty("nsyear"));
            System.out.println();
            System.out.println("Student Event Record");
            ArrayList<Event> es = admintracker.getStudentEvents(s);
            for(Event e : es)
                System.out.println(e.toString() +", for " + e.get(s.getID()));
            System.out.println("End Record List");
            System.out.println("Student Ribbon List");
            System.out.println("M D D H C U A 4 3 2 1 E A E P P U C D C R O R B S");
            for(int i = 0; i < 25; i++)
            {
                System.out.print(s.getRibbons().get(i) + " ");
            }
            System.out.println("\nEnd Ribbon List");
            
        }
        else if(command.equals("2") || command.equals("edit info"))
        {
            System.out.println("Remove this student and create a new one.");
        }
        else if(command.equals("3") || command.equals("remove"))
        {
            System.out.print("Are you sure you want to remove the entry for " + s.getName() + " (yes/no)?");
            boolean go = scanner.nextLine().equals("yes");
            if(go)
            {
                Student rem = s;
                admintracker.removeStudent(rem.getLastName(), rem.getFirstName(), rem.getID());
                s = null;
                System.out.println("Student removed.");
                return false;
            }
            System.out.println("Operation canceled.");
            return true;
        }
        return true;
    }
    
    private static boolean events()
    {
        do
        {
            System.out.println("Events Menu");
            System.out.println("Verb?");
            System.out.println("1. Get All");
            System.out.println("2. Create");
            System.out.println("3. Edit / Input");
            System.out.println("4. Cancel");
            System.out.println("Verb> ");
        } while(eventLoop());
        return true;       
    }
    
    private static boolean eventLoop()
    {
        String command = getCommand();
        if(command.equals("4") || command.equals("cancel") || command.equals("\\c"))
        {
            return false;
        }
        else if(command.equals("1") || command.equals("get all"))
        {
            ArrayList<Event> events = admintracker.getEvents();
            System.out.println("Printing all events and dates:");
            for(Event e : events)
                System.out.println("NAME: " + e.getName() + " DATE: " + e.getDate());
            System.out.println("End Event List");
        }
        else if(command.equals("2") || command.equals("create"))
        {
            System.out.print("(1/5) Name? ");
            String name = scanner.nextLine();
            if(name.equals("\\c")) return true;
            System.out.print("(2/5) Date (yyyy-mm-dd)? ");
            String date = scanner.nextLine();
            if(date.equals("\\c")) return true;
            System.out.print("(3/5) Category? ");
            String cate = scanner.nextLine().toUpperCase();
            if(cate.equals("\\c")) return true;

            String dura = "1";
            if(cate.indexOf("SERVICE") != -1)
            {
                System.out.print("(4/5) Duration? ");
                dura = scanner.nextLine();
                if(dura.equals("\\c")) return true;
            }

            String cou2 = (cate.indexOf("SERVICE") != -1) ? "HOURS" : "ATTENDED";
            int dur2 = Integer.parseInt(dura);

            Event e = admintracker.createEvent(name, date, cate, cou2, dur2);
            System.out.println("Event successfully created.");
            System.out.print("(5/5) Would you like to input students now (yes/no)? ");
            String choice = scanner.nextLine();
            if(choice.equals("yes")) eventEdit(e);
        }
        else if(command.equals("3") || command.equals("edit") || command.equals("input"))
        {
            System.out.print("(1/3) Name? ");
            String name = scanner.nextLine();
            if(name.equals("\\c")) return true;
            System.out.print("(2/3) Date? ");
            String date = scanner.nextLine();
            if(date.equals("\\c")) return true;

            Event e = admintracker.getEvent(name, Date.valueOf(date));
            if(e == null)
            {
                System.out.println("Event doesn't exist! Make sure your information is correct.");
                return true;
            }
            else
            {
                do
                {
                    System.out.println(e.getName() + ", on " + e.getDate());
                    System.out.println("Event Edit Menu");
                    System.out.println("1. Get Info");
                    System.out.println("2. Edit Info");
                    System.out.println("3. Input");
                    System.out.println("4. Remove");
                    System.out.println("5. Cancel");
                    System.out.println("Edit> ");
                } while(eventEdit(e));
            }
        }
        return true;
    }
    
    private static boolean eventEdit(Event e)
    {
        String command = getCommand();
        if(command.equals("5") || command.equals("cancel") || command.equals("\\c"))
        {
            return false;
        }
        else if(command.equals("1") || command.equals("get info"))
        {
            System.out.println("Name: " + e.getName() + " | Date: " + e.getDate());
            System.out.println("Category: " + e.getCategory() + " | Counting: " + e.getProperties().getProperty("counting"));
            System.out.println("Duration: " + e.getProperties().getProperty("countingvalue"));
            System.out.println("List of students attending this event:");
            for(Student s : admintracker.getEventStudents(e))
            {
                System.out.println(s +", for " + e.get(s) + e.getProperties().getProperty("counting"));
            }
            System.out.println("End attendance list");
        }
        else if(command.equals("2") || command.equals("edit info"))
        {
            System.out.println("Please remove this event and create a new one.");
            System.out.println("There are currently no facilities to handle editing.");
        }
        else if(command.equals("3") || command.equals("input"))
        {
            System.out.println("You are inputting values for: " + e.toString());
            System.out.println("Tip: to remove credit, input a zero.");
            if(e.getCategory().equals(Category.COMMUNITY_SERVICE) ||
               e.getCategory().equals(Category.UNIT_SERVICE))
            {
                System.out.println("There are two methods to input hours. Which one?");
                System.out.println("1. Input each student individually");
                System.out.println("2. Input one time for all students");
                System.out.println("Choice> ");
                String ch = getCommand();
                if(ch.equals("1") || ch.equals("individually"))
                {
                    eventInput(e, true);
                }
                else if(ch.equals("2") || ch.equals("one time"))
                {
                    eventInput(e, false);
                }
                return true;
            }
            else
            {
                System.out.println("There is only one method to input attendance: 1/0");
                eventInput(e, true);      
            }
        }
        else if(command.equals("4") || command.equals("remove"))
        {
            System.out.print("Are you sure you want to remove the entry for " + e.getName() + ", on " + e.getDate() + " (yes/no)?");
            boolean go = scanner.nextLine().equals("yes");
            if(go)
            {
                Event re = e;
                admintracker.removeEvent(re.getName(), re.getDate());
                e = null;
                System.out.println("Event removed.");
                return false;
            }
            System.out.println("Operation canceled.");
            return true;
        }
        return true;
    }
    
    private static boolean eventInput(Event e, boolean individual)
    {
        boolean stopped = false;
        int allValue = 0;
        System.out.println("Type STOP to end.");
        if(!individual)
        {
            System.out.println("Enter the time to apply to all students.");
            System.out.println("Hours> ");
            String command = getCommand();
            if(command.matches("\\d{1,}"))
            {
                allValue = Integer.parseInt(command);
            }
            else
            {
                System.out.println("Not a number.");
                return true;
            }
        }   
        while(!stopped)
        {
            System.out.print("(4/4) Student (ID/Name)? ");
            String id = scanner.nextLine();
            if(id.toUpperCase().equals("STOP")) break;
            id = id.toUpperCase();

            Student s = null;
            if(id.matches("\\d{4,}"))
            {
                s = admintracker.getStudent("", "", Integer.parseInt(id));
            }
            else if(id.matches("(\\w|\\s)++"))
            {
                if(admintracker.getStudents(id, "", -1).size() +
                   admintracker.getStudents("", id, -1).size() == 1)
                {
                    Student sa = admintracker.getStudent(id, "", -1);
                    Student sb = admintracker.getStudent("", id, -1);
                    s = sa;
                    if(s == null) s = sb;
                }
                else
                {
                    System.out.println("Ambiguous name, entries below:");
                    System.out.println("BEGIN AMBIGUOUS LIST");
                    ArrayList<Student> a = admintracker.getStudents(id, "", -1);
                    ArrayList<Student> b = admintracker.getStudents("", id, -1);
                    for(Student sp : a)
                        System.out.println(sp);
                    for(Student sq : b)
                        System.out.println(sq);
                    System.out.println("END AMBIGUOUS LIST");
                }
            }      

            if(s == null)
            {
                System.out.println("Student does not exist!");
            }
            else
            {        
                int stu = s.getID();
                if(e.getCategory().toString().equals("COMMUNITY SERVICE") ||
                        e.getCategory().toString().equals("UNIT SERVICE"))
                {
                    int v = 0;
                    if(individual)
                    {
                        System.out.print("(4/4) Hours? ");
                        String va = scanner.nextLine();
                        if(va.matches("\\d{1,}"))
                            v = Integer.parseInt(va);
                        else
                        {
                            System.out.println("Not a number!");
                            v = 0;
                        }
                    }
                    else
                    {
                        v = allValue;
                    }
                    admintracker.addCredit(e, stu, v);
                }
                else
                {
                    admintracker.addCredit(e, stu, 1);
                }
            }
        }
        if(!individual) System.out.println("The value: " + allValue + " was applied to all students.");
        return true;
    }
    
    private static boolean awards()
    {
        do
        {
            System.out.println("Awards Menu");
            System.out.println("Verb?");
            System.out.println("1. Output Awards");
            System.out.println("2. Input");
            System.out.println("3. Cancel");
            System.out.println("Verb> ");
        } while(awardLoop());
        return true;
    }
    
    private static boolean awardLoop()
    {
        String command = getCommand();
        if(command.equals("3") || command.equals("cancel") || command.equals("\\c"))
        {
            return false;
        }
        else if(command.equals("1") || command.equals("output awards"))
        {
            outputAwards();
        }
        else if(command.equals("2") || command.equals("input"))
        {
            awardInput();
        }
        return true;
    }
    
    private static boolean outputAwards() //possible sorts: category, platoon, student, 
    {
        System.out.println("different printing sorts coming soon!");
        System.out.print("(1/1) Category? ");
        String c = scanner.nextLine();
        if(c.equals("\\c")) return true;
        
        System.out.println("BEGIN AWARDS");
        if(c.equals("ALL"))
        {
            String[] cats = {"DRILL", "ACADEMICS", "COLOR GUARD", "PT",
                             "ORIENTEERING", "MARKSMANSHIP", "UNIT SERVICE",
                             "COMMUNITY SERVICE", "PARTICIPATION", "MINI BOOT CAMP",
                             "SEA CRUISE"};
            for(String t : cats)
            {
                Category c2 = Category.parseCategory(t);
                for(String s: admintracker.getAward(c2))
                    System.out.println(s);
            }
        }
        else
        {
            Category c2 = Category.parseCategory(c);
            for(String s : admintracker.getAward(c2))
                System.out.println(s);
        }
        System.out.println("END AWARDS");
        
        return true;
    }
    
    private static boolean awardInput()
    {
        do
        {
            System.out.println("Award Input System");
            System.out.println("1. Input award information");
            System.out.println("2. Cancel");
            System.out.println("Input> ");
        } while (ribbonInput());
        return true;
    }
    
    private static boolean ribbonInput()
    {
        String command = getCommand();
        if(command.equals("2") || command.equals("cancel") || command.equals("\\c"))
            return false;
        System.out.println("Provide student information:");
        Student s = getStudent();
        if(s == null) return false;
        System.out.println("You are about to enter ribbon information for: " + s.getName() +", " + s.getID());
        if(s.getRibbons().hasNoData())
        {
            System.out.println("Inserting new information...");
            
        }
        else
        {
            System.out.println("[HEY!] This student has previous data!");
        }
        System.out.println("Type yes to continue.> ");
        boolean go = getCommand().equals("yes");
        if(go)
        {
            System.out.println("Type out one massive line, separated by spaces.");
            System.out.println("M D D H C U A 4 3 2 1 E A E P P U C D C R O R B S");
            String line = scanner.nextLine();
            StringTokenizer stk = new StringTokenizer(line);
            int[] vals = new int[25];
            for(int i = 0; i < vals.length; i++)
            {
                if(stk.hasMoreElements())
                {
                    String val = stk.nextToken();
                    if(val.matches("\\d{1,}"))
                        vals[i] = Integer.parseInt(val) + 1;
                    else if(val.equals("`"))
                        vals[i] = 0;
                    else vals[i] = 0;
                }
                else
                {
                    vals[i] = 0;
                }
                
            }
            RibbonData rd = new RibbonData(s.getID(), vals);
            s.setRibbons(rd);
            System.out.println("Ribbons set for " + s);
            
        }
        return true;
    }
    
    private static void ribbons1(Student s)
    {
        int[] vals = new int[25];
            String[] arr = {"ma", "du", "dc", "hc", "ca", "ua", "ap", "n4", "n3",
                        "n2", "n1", "ec", "aa", "ep", "pf", "pa", "us", "cs",
                        "dt", "cg", "rt", "oi", "re", "bl", "sc"};
            for(int i = 0; i < vals.length; i++)
            {
                System.out.print("("+i+"/25)"+arr[i] +"> ");
                String val = scanner.nextLine();
                if(val.matches("\\d{1,}"))
                    vals[i] = Integer.parseInt(val) + 1;
                else if(val.equals("`"))
                    vals[i] = 0;
                else vals[i] = 0;
                
            }
            RibbonData rd = new RibbonData(s.getID(), vals);
            s.setRibbons(rd);
            System.out.println("Ribbons set for " + s);
    }

    private static boolean help()
    {
        System.out.println("Help for AdminTracker:");
        System.out.println("AdminTracker operates by typing commands into the command line.");
        System.out.println("There is a basic set of commands:");
        System.out.println("help, quit, restart, save");
        System.out.println("However, to access the main functions, you need to type a subject,");
        System.out.println("followed by a verb.");
        System.out.println("These are the subjects:");
        System.out.println("events, students, awards");
        System.out.println("After you type this, it will prompt for a verb.");
        System.out.println("Some of these verbs are:");
        System.out.println("get, remove, set");
        System.out.println("After you type this, follow the instructions to complete the command.");
        System.out.println("");
        System.out.println("For more information, see your Battalion Roster Officer.");
        return true;
    }
    
    private static String getCommand()
    {
        String command = scanner.nextLine();
        command = command.trim();
        command = command.toLowerCase();
        return command;
    }
    
    private static Student getStudent()
    {
        System.out.print("(1/1) Student (ID/Name)? ");
        String id = scanner.nextLine();
        if(id.equals("\\c")) return null;
        id = id.toUpperCase();

        Student s = null;
        if(id.matches("\\d{4,}"))
        {
            s = admintracker.getStudent("", "", Integer.parseInt(id));
        }
        else if(id.matches("(\\w|\\s)++"))
        {
            if(admintracker.getStudents(id, "", -1).size() +
               admintracker.getStudents("", id, -1).size() == 1)
            {
                Student sa = admintracker.getStudent(id, "", -1);
                Student sb = admintracker.getStudent("", id, -1);
                s = sa;
                if(s == null) s = sb;
            }
            else
            {
                System.out.println("Ambiguous name, possible entries below:");
                ArrayList<Student> a = admintracker.getStudents(id, "", -1);
                ArrayList<Student> b = admintracker.getStudents("", id, -1);
                for(Student sp : a)
                    System.out.println(sp);
                for(Student sq : b)
                    System.out.println(sq);
                System.out.println("End Student List");
            }
        }
        if(s == null)
        {
            System.out.println("Student doesn't exist, or there are multiple entries!");
            return null;
        }
        return s;
    }
        
}
